package com.yanqu.road.dao.impl.activity.douluodalu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.douluodalu.DouLuoDaLuAttackInfo;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuHelpRecordData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DouLuoDaLuHelpRecordDataDaoImpl extends TempDataObjectDao<DouLuoDaLuHelpRecordData> {


    public Long getMaxRecordId(int activityId, int groupId) {
        String sql = "select ifnull(max(id),0) as maxId from t_c_douluodalu_help_record where `activityId`=? and groupId=?";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            int i = 1;
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(i++, activityId);
            call.setLong(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }


    public static String getDouLuoDaLuHelpRecordDataSql = "select * from `t_c_douluodalu_help_record` where `activityId`=? and groupId=? order by `recordTime` desc";
    public List<DouLuoDaLuHelpRecordData> getDouLuoDaLuHelpRecordData(long activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<DouLuoDaLuHelpRecordData> result = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDouLuoDaLuHelpRecordDataSql);
            int i = 1;
            call.setLong(i++, activityId);
            call.setLong(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                DouLuoDaLuHelpRecordData helpRecordData = parse(rs);
                if(helpRecordData.getKilledUserId() == -1){
                    continue;
                }
                result.add(helpRecordData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertDouLuoDaLuHelpRecordDataSql = "insert into `t_c_douluodalu_help_record`(id,activityId,userId,regionId,eventId,targetId,targetBlood,targetTotalBlood," +
            "helpAttackMap,killedUserId,isReceived,reward,hunHuan,recordTime,eventTime,killTime,groupId) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DouLuoDaLuHelpRecordData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRegionId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTargetId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTargetBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTargetTotalBlood()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getHelpAttackMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getKilledUserId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReceived()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getHunHuan()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRecordTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventTIme()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getKillTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(insertDouLuoDaLuHelpRecordDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDouLuoDaLuHelpRecordDataSql = "update `t_c_douluodalu_help_record` set userId=?,regionId=?,eventId=?,targetId=?,targetBlood=?,targetTotalBlood=?," +
            "helpAttackMap=?,killedUserId=?,isReceived=?,reward=?,hunHuan=?,recordTime=?,eventTime=?,killTime=? where activityId=? and id=? and groupId=?";

    @Override
    protected boolean updateData(DouLuoDaLuHelpRecordData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRegionId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTargetId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTargetBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTargetTotalBlood()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getHelpAttackMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getKilledUserId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReceived()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getHunHuan()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRecordTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventTIme()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getKillTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(updateDouLuoDaLuHelpRecordDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DouLuoDaLuHelpRecordData parseData(ResultSet rs) throws SQLException {
        DouLuoDaLuHelpRecordData data = new DouLuoDaLuHelpRecordData();
        data.setId(rs.getLong("id"));
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setRegionId(rs.getInt("regionId"));
        data.setEventId(rs.getInt("eventId"));
        data.setTargetId(rs.getLong("targetId"));
        data.setTargetBlood(rs.getLong("targetBlood"));
        data.setTargetTotalBlood(rs.getLong("targetTotalBlood"));
        data.setHelpAttackMap(JSON.parseObject(rs.getString("helpAttackMap"), new TypeReference<ConcurrentHashMap<Long, DouLuoDaLuAttackInfo>>() {}));
        data.setKilledUserId(rs.getLong("killedUserId"));
        data.setIsReceived(rs.getBoolean("isReceived"));
        data.setReward(rs.getString("reward"));
        data.setHunHuan(rs.getString("hunHuan"));
        data.setRecordTime(rs.getLong("recordTime"));
        data.setEventTIme(rs.getInt("eventTime"));
        data.setKillTime(rs.getLong("killTime"));
        data.setGroupId(rs.getInt("groupId"));

        return data;
    }
}
